package org.tof.hotel;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.tof.hotel.csv.CSVWriter;
import org.tof.hotel.entity.Administrator;
import org.tof.hotel.entity.Application;
import org.tof.hotel.entity.Customer;
import org.tof.hotel.entity.Room;

import java.util.*;

/** Runner class */
public class Main {

    static Logger logger = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) {
        DOMConfigurator.configure("D:\\education\\epam\\web-project\\hotel\\log4j.xml");
    }

    public static void console(){
        Scanner scanner = new Scanner(System.in);

        System.out.println("----------------------initial initialization----------------------");
        Customer customer = new Customer("Adam","+380 44 777 77 77", "adam@eden.garden");
        Administrator administrator = new Administrator("Eva","+380 44 666 66 66", "eva@eden.garden");
        Room room = new Room(1, RoomLevel.STANDARD);
        Application application = new Application(2,RoomLevel.STANDARD, customer, new Date(), new Date(), administrator);
        logger.debug("generating more entities");
        customer.generate(5);
        administrator.generate(2);
        room.generate(4);
        application.generate(4);
        System.out.println("------------------------------------------------------------------");

        logger.debug("GenerateSomeCsvFiles");
        String dirr = "D:\\education\\epam\\web-project\\hotel\\generated-files\\";
        CSVWriter csvWriter = CSVWriter.getInstance();
        csvWriter.setPathname(dirr + "administrators.csv");
        csvWriter.writeBean(administrator);
        csvWriter.setPathname(dirr + "customers.csv");
        csvWriter.writeBean(customer);
        csvWriter.setPathname(dirr + "applications.csv");
        csvWriter.writeBean(application);
        csvWriter.setPathname(dirr + "rooms.csv");
        csvWriter.writeBean(room);
        logger.debug("EndGeneratingSomeCsvFiles");

        System.out.println("Please choose a language:");
        System.out.println("1 - english");
        System.out.println("2 - german");
        System.out.println("3 - ukrainian");
        System.out.println("4 - russian");
        System.out.println("5 - terminate program");
        char i = 0;
        Locale curLoc = null;
        boolean noCorrectInput = true;
        while (noCorrectInput){
            noCorrectInput = false;
            try {
                //i = (char)System.in.read(); //TODO implement in another way
                //scanner.nextInt();
                //i = (char)scanner.nextInt();
                logger.trace("user input: " + i);
            } catch (Exception e) {
                logger.warn(" exception", e);//TODO implement exceptions handling
            }
            switch(i){
                case '1':
                    curLoc = Locale.US;
                    break;
                case '2':
                    curLoc = Locale.GERMANY;
                    break;
                case '3':
                    curLoc = new Locale("ua","UA");
                    break;
                case '4':
                    curLoc = new Locale("ru","RU");
                    break;
                case '5':
                    return;
                default:
                    noCorrectInput = true;
                    System.out.println("incorrect input (" + i + "), please try again");
            }
        }

        ResourceBundle rbConsole = null;
        try {
            rbConsole = ResourceBundle.getBundle("console",curLoc);
        } catch (MissingResourceException e) {
            logger.error(e.getMessage(), e);
            logger.fatal("Terminating program execution");
            System.out.println("Critical error occurred, program has been terminated");
            return; //TODO check if instead of terminating program rbConsole = ResourceBundle.getDefaultBundle() can be used
        }

        int curHour = (new Date()).getHours();
        if(curHour > 8 && curHour < 12)
            System.out.println(rbConsole.getString("good_morning"));
        else if(curHour >= 12 && curHour < 17)
            System.out.println(rbConsole.getString("good_afternoon"));
        else if(curHour >= 17 && curHour < 22)
            System.out.println(rbConsole.getString("good_evening"));
        else System.out.println(rbConsole.getString("hello"));

        System.out.println("Choose an action:");
        System.out.println("1 - dump all administrators to csv file");
        System.out.println("2 - dump all customers to csv file");
        System.out.println("3 - dump all applications to csv file");
        System.out.println("4 - dump all rooms to csv file");
        System.out.println("5 - exit");

        String dir = "D:\\education\\epam\\web-project\\hotel\\generated-files\\";
        noCorrectInput = true;
        while (noCorrectInput) {
            try {
                //System.in.read(); //TODO implement in another way
                i = (char)scanner.nextInt();
                logger.trace("user input: " + i);
            } catch (Exception e) {
                logger.warn(" exception", e);//TODO implement exceptions handling
            }
            switch(i){
                case '1':
                    csvWriter.setPathname(dir + "administrators.csv");
                    csvWriter.writeBean(administrator);
                    break;
                case '2':
                    csvWriter.setPathname(dir + "customers.csv");
                    csvWriter.writeBean(customer);
                    break;
                case '3':
                    csvWriter.setPathname(dir + "applications.csv");
                    csvWriter.writeBean(application);
                    break;
                case '4':
                    csvWriter.setPathname(dir + "rooms.csv");
                    csvWriter.writeBean(room);
                    break;
                case '5':
                    return;
                default:
                    noCorrectInput = true;
                    System.out.println("incorrect input, please try again");
                    break;
            }
        }
    }
}

